package com.hupu.selenium.jop.util;

import java.net.URLDecoder;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import org.eclipse.jetty.util.UrlEncoded;

import com.google.gson.Gson;

public class PreventLinkUtil {
	
	private final static String PREVENT_LINK_SERVER = PropertiesUtils.getProperties().getProperty("hupu.prevent.link.url")+"/moo/preventlink/data/link";
	
	public static String getPreventLink(Integer plType, Integer cdnSPType, String key, String stream, long time) throws Exception{
		
		StringBuffer sb = new StringBuffer(PREVENT_LINK_SERVER);
		sb.append("?");
		sb.append("plType="+plType+"&");
		sb.append("cdnSPType="+cdnSPType+"&");
		sb.append("key="+UrlEncoded.encodeString(key)+"&");
		sb.append("stream="+UrlEncoded.encodeString(stream)+"&");
		sb.append("time="+time);
		
		String result = HttpClientUtils.httpGet(sb.toString());
		System.out.println("Prevent Link Type:"+plType+"\nPrevent Llink Request url:"+sb.toString()+"\nPrevent Llink Request result:"+result+"\n");
		Map<Object,Object> map = (new Gson()).fromJson(result, Map.class);
		if(200.0 != (Double)map.get("status"))
			throw new Exception("防盗链请求异常："+sb.toString());
		return (String) map.get("data");
	}
	
	public static void main(String[] args) throws Exception {
		System.out.println(new Date().getTime());
		System.out.println(UUID.randomUUID().toString().replaceAll("-", ""));
		System.out.println(new Date(1418725488554l));;
		System.out.println(PreventLinkUtil.getPreventLink(1,2, "test_lanxun_pull", "123", new Date().getTime()));
		System.out.println("rtmp://pull2.arenazb.hupu.com/test?"+URLDecoder.decode( PreventLinkUtil.getPreventLink(1,2, "test_lanxun_pull", "123", new Date().getTime()))+"/123");
	}

}
